package com.cuckoo.cuckoo_app2.ui.home;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cuckoo.cuckoo_app2.MyApplication;
import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.adapters.DashboadrdPlatformAdpter;
import com.cuckoo.cuckoo_app2.adapters.HomeTipsMessagAdapter;
import com.cuckoo.cuckoo_app2.business.Imp.PlatformBusinessImp;
import com.cuckoo.cuckoo_app2.business.Imp.TipsMessagBusinessImp;
import com.cuckoo.cuckoo_app2.business.PlatformBusiness;
import com.cuckoo.cuckoo_app2.business.TipsMessagBusiness;
import com.cuckoo.cuckoo_app2.dao.Platform;
import com.cuckoo.cuckoo_app2.dao.TipsMessag;
import com.cuckoo.cuckoo_app2.dao.User;
import com.cuckoo.cuckoo_app2.model.IntentResponseModel;
import com.cuckoo.cuckoo_app2.ui.WebViewActivity;
import com.cuckoo.cuckoo_app2.ui.dashboard.DashboardFragment;
import com.cuckoo.cuckoo_app2.utils.DialogUtils;
import com.cuckoo.cuckoo_app2.utils.LocalBroadCodeUtils;
import com.cuckoo.cuckoo_app2.utils.RouteUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HomeFragment extends Fragment {

    private RecyclerView recyclerView;
    private HomeTipsMessagAdapter adapter;
    private List<TipsMessag> dataList = new ArrayList<>();
    private ConstraintLayout constraintLayout;

    private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            Log.i("LocalBroadcastReceiver", action);

            if (action.equals(LocalBroadCodeUtils.LOCAL_BROADCAST_TASK_REFRESH)) {
                IntentResponseModel model = (IntentResponseModel) intent.getSerializableExtra("model");
                switch (model.getCode()) {
                    case LocalBroadCodeUtils.LOCAL_BROADCAST_TASK_REFRESH:
                        getData();
                        updateRecyclerViewData();
                        break;
                }
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Log.i("TEST", "HomeFragment.onCreate");
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i("TEST", "HomeFragment.onCreateView");
        // 加载 Fragment 的布局
        return inflater.inflate(R.layout.fragment_home, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Log.i("TEST", "HomeFragment.onViewCreated");
        super.onViewCreated(view, savedInstanceState);

        constraintLayout = view.findViewById(R.id.not_data);

        // 注册BroadcastReceiver
        IntentFilter intentFilter = new IntentFilter(LocalBroadCodeUtils.LOCAL_BROADCAST_TASK_REFRESH);
        LocalBroadcastManager.getInstance(getContext()).registerReceiver(myBroadcastReceiver, intentFilter);

        // 初始化 RecyclerView
        recyclerView = view.findViewById(R.id.recycler_home);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        // 初始化适配器并设置给 RecyclerView
        adapter = new HomeTipsMessagAdapter(getContext(), dataList, new ButtonClickListener());
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onStart() {
        Log.i("TEST", "HomeFragment.onStart");
        super.onStart();

        // 创建数据
        getData();
        updateRecyclerViewData();

    }

    @Override
    public void onResume() {
        Log.i("TEST", "HomeFragment.onResume");
        super.onResume();

        // 注册BroadcastReceiver以接收广告广播
        IntentFilter intentFilter = new IntentFilter();
        // 跳转登录页面
        intentFilter.addAction(LocalBroadCodeUtils.LOCAL_BROADCAST_TASK_REFRESH);
        LocalBroadcastManager.getInstance(getContext()).registerReceiver(myBroadcastReceiver, intentFilter);
    }

    @Override
    public void onPause() {
        Log.i("TEST", "HomeFragment.onPause");
        super.onPause();

        // 注销BroadcastReceiver
        LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(myBroadcastReceiver);
    }

    @Override
    public void onStop() {
        Log.i("TEST", "HomeFragment.onStop");
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        Log.i("TEST", "HomeFragment.onDestroyView");
        super.onDestroyView();
        // 注销BroadcastReceiver
        LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(myBroadcastReceiver);
    }

    public void getData() {
        TipsMessagBusiness messagBusiness = new TipsMessagBusinessImp();
        dataList = messagBusiness.getAll();
        if(dataList.size() > 0){
            constraintLayout.setVisibility(View.GONE);
        }else {
            constraintLayout.setVisibility(View.VISIBLE);
        }

    }

    public void clearRecyclerViewData() {
        if (adapter != null) {
            adapter.clearData(); // 调用适配器的方法来清空数据
        }
    }

    public void updateRecyclerViewData() {
        if (adapter != null) {
            adapter.updateData(dataList); // 调用适配器的方法来更新数据
        }
    }

    public class ButtonClickListener {

        public void onEnter(String id) {
            // 检测当前是否可用
            User user = MyApplication.getInstances().getUser();
            if (user == null || user.getIsVip() == 0) {
                DialogUtils.showToast(getContext(), "非会员不能操作");
                return;
            }
            // 查询跳转
            PlatformBusiness platformBusiness = new PlatformBusinessImp();
            Platform platform = platformBusiness.getById(id);
            if (platform.getHasTips().equals(1)) {
                platform.setHasTips(0);
                platformBusiness.updata(platform);
            }

            RouteUtils.toWebViewActivity(getContext(), id, "enter");
        }
    }

}